﻿using System;
using System.Collections.Generic;
using System.Web.SessionState; //第一步：导入此命名空间
//ashx文件要使用Session,必须实现Session接口;
using System.Linq;
using System.Web;
using System.IO;
using HKEA.CMS.Core.Utility;

using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.BLL.Tbl;


namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// MediaRoomVideo 的摘要说明
    /// </summary>
    public class MediaRoomVideo : IHttpHandler, IRequiresSessionState //第二步：实现接口 到此就可以使用Session了
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string type = context.Request.QueryString["type"];

            if (!string.IsNullOrEmpty(type))
            {
                switch (type)
                {
                    case "1"://最后一个MediaRoom的UID
                        GetLastMediaRoomUID(context);
                        break;
                    case "2"://获取最后一个MediaRoom的html
                        DeleteOneMediaRoom(context);
                        break;
                    case "3"://获取指定UID的MediaRoom的html
                        //GetOneMediaRoomByUID(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetLastMediaRoomUID(HttpContext context)
        {
            string ret = "<tr id='tr_mediaroom_{0}'><td><object type='application/x-shockwave-flash' data='/Media/vcastr/vcastr3.swf' width='100' height='75' id='vcastr3'><param name='allowFullScreen' value='true' /><param name='FlashVars' value='xml=<vcastr><channel><item><source>{1}</source></item></channel><config><defautVolume>0</defautVolume><controlPanelMode>float</controlPanelMode><isAutoPlay>false</isAutoPlay><isLoadBegin>true</isLoadBegin><isRepeat>false</isRepeat></config></vcastr>' /></object></td><td>{2}</td><td><a href='javascript:DeleteMediaRoom({3})'><img src='Media/Css/images/del-form.png' alt='delete' /></a></td></tr>";
            tblMediaRoom ent = new tblMediaRoom();
            ent = tblMediaRoomBLL.GetInstance().GetLastEntity();
            if (ent != null) {
                string video_url = ent.FilePath + ent.FileName + ent.FileExt;
                ret = string.Format(ret, ent.MediaRoomUID, video_url,ent.AliasName, ent.MediaRoomUID);
            }     
            context.Response.Write(ret);
        }

        private void DeleteOneMediaRoom(HttpContext context)
        {
            int ret = 0;
            string mediaroomUID = context.Request.QueryString["mediaroomUID"];
            string fileName = string.Empty;
            string customerUID = context.Request.QueryString["Cuid"];
            if (!string.IsNullOrEmpty(mediaroomUID))
            {
                tblMediaRoom entMediaroom = new tblMediaRoom();
                entMediaroom = tblMediaRoomBLL.GetInstance().GetEntity(int.Parse(mediaroomUID));
                string videoUrl = entMediaroom.FilePath + entMediaroom.FileName + entMediaroom.FileExt;
                fileName = entMediaroom.FileName;
                File.Delete(context.Server.MapPath(videoUrl));//删除文件

                ret = tblMediaRoomBLL.GetInstance().DeleteENT(entMediaroom);//删除记录
            }
            if (Convert.ToInt32(ret) == 1)
            {
                string info = string.Format("FileName:{0},have been delete", fileName);
                WriteToLog(Convert.ToInt32(customerUID), info);
            }
            context.Response.Write(ret);
        }
        protected void WriteToLog(int customerUID, string info)
        {
            BasePage page = new BasePage();
            tblUser ent = page.GetManageFromSession();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(customerUID);
            tblHistory entlog = new tblHistory();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 42;
            entlog.CustomerUID = customerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = ent.User_Name + " {" + Company.Name_Eng + "}{MediaRoomVideo}" + info;
            entlog.LastModifyBy = ent.User_Name;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}